package com.naizhuai.ui.rank;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.ui.BaseFragment;
import com.naizhuai.ui.adapter.ManageDividerItemDecoration;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.User.RankData;
import com.naizhuai.ui.entity.User.UserRank;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.naizhuai.util.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public class TalentDataFragment extends BaseFragment {
    public static final String OPTION_KEY = "option_key";
    public static final String DATE_KEY = "date_key";
    @BindView(R.id.dataList)
    RecyclerView dataList;
    Unbinder unbinder;
    private UserManageDataApiStoreIml mDataApiStoreIml;
    private DataAdapter mDataAdapter;
    public TalentDataFragment() {

    }

    public static TalentDataFragment newInstance(String optionkey,String datekey) {
        TalentDataFragment fragment = new TalentDataFragment();
        Bundle arguments = new Bundle();
        arguments.putString(OPTION_KEY, optionkey);
        arguments.putString(DATE_KEY, datekey);
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mDataApiStoreIml == null) {
            mDataApiStoreIml = UserManageDataApiStoreIml.getInstance();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_talent_data, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dataList.addItemDecoration(new ManageDividerItemDecoration(getActivity(), ManageDividerItemDecoration.VERTICAL_LIST, 10, true));
        dataList.setAdapter(mDataAdapter=new DataAdapter());
        if (getArguments().containsKey(OPTION_KEY)) {
            String key = getArguments().getString(OPTION_KEY);
            String dateKey=getArguments().getString(DATE_KEY);
            showWaitDiaLog();
            mDataApiStoreIml.getPerson_Rank(key,dateKey)
                    .subscribe(new NetWorkObserver<UserRank>() {
                        @Override
                        protected void onFail(String errormsg) {
                            hideWaitDialog();
                            shortToast(errormsg);
                        }

                        @Override
                        protected void onSuccess(UserRank result) {
                            hideWaitDialog();
                            mDataAdapter.setData(result.rankData);
                        }
                    });
        }
    }

    class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
        private List<RankData> mRankDataList=new ArrayList<>();

        public void setData(List<RankData> rankDataList) {
            mRankDataList = rankDataList;
            notifyDataSetChanged();
        }

        public DataAdapter() {
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.talent_item_layout, parent, false);

            return new  ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                RankData rankData=mRankDataList.get(holder.getAdapterPosition());
            if (position>2){
                holder.rankTag.setVisibility(View.GONE);
            }else {
                holder.rankTag.setVisibility(View.VISIBLE);
            }
            holder.userRank.setText(rankData.rank);
            holder.teamname.setText(rankData.departmentName);
            holder.userCount.setText(rankData.totalCount);
            holder.userName.setText(rankData.username);
            holder.userAvatar.post(() -> ViewUtils.showAvatorOrName(rankData.userThumb, rankData.username, holder.userAvatar));
        }

        @Override
        public int getItemCount() {
             return mRankDataList.size();
        }

         class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.user_rank)
            TextView userRank;
            @BindView(R.id.user_avatar)
            CircleImageView userAvatar;
            @BindView(R.id.user_name)
            TextView userName;
            @BindView(R.id.teamname)
            TextView teamname;
            @BindView(R.id.user_count)
            TextView userCount;
             @BindView(R.id.rank_tag)
             View rankTag;
            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
